package com.example.batterynotification;

import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

public class CustomSwitchPrefrence extends Preference {
	String key = null;
	OncustomSwitchChangeListener mListener;

	public CustomSwitchPrefrence(Context context, AttributeSet attrs) {
		super(context, attrs);
		key = getKey();
	}

	@Override
	protected View onCreateView(ViewGroup parent) {
		LayoutInflater li = (LayoutInflater) getContext().getSystemService(
				Context.LAYOUT_INFLATER_SERVICE);
		View view = li.inflate(R.layout.custom_switch_prefrence_layout, parent,
				false);

		CustomSwitch customSwitch = (CustomSwitch) view
				.findViewById(R.id.customSwitch);

		customSwitch.setChecked(Preferences.getBoolPreferences(key));
		customSwitch.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Preferences.savePreferences(key, ((CustomSwitch) v).isChecked());
				if(mListener!=null)
					mListener.onEvent(((CustomSwitch) v).isChecked());
			}
		});

		return view;
	}

	public void setOnCustomSwitchChangeListener(
			OncustomSwitchChangeListener eventListener) {
		mListener = eventListener;
	}

}
